//
//  ViewController.m
//  矿机
//
//  Created by 大王叫我来巡山 on 2021/2/22.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>



@interface ViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic,strong)WKWebView *wkWebView;
@property (strong,nonatomic)UIProgressView *progressView;
@property (nonatomic,strong)UIButton *backButton;
@end
#define  DEVICE_HEIGHT    [UIScreen mainScreen].bounds.size.height
#define  DEF_NAV_HEIGHT      (DEVICE_HEIGHT == 812 || DEVICE_HEIGHT == 896 || DEVICE_HEIGHT == 844 || DEVICE_HEIGHT == 926 ? 88.f : 64.f)

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSLog(@"123132123");
    NSString *formalStrUrl = @"https://kj.eatmain.com/#/";
    NSString *testStrUrl = @"https://testkj.eatmain.com/";
    
    
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor],
                                                                            NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton addTarget:self action:@selector(gotoBack) forControlEvents:UIControlEventTouchUpInside];
    CGSize size                                     =self.backButton.currentBackgroundImage.size;
    self.backButton.bounds                               = CGRectMake(0, 0,  40,  40);    self.backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
   
    

    
    self.wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-44)];
    [self.wkWebView setOpaque:YES];
    self.wkWebView.backgroundColor = [UIColor whiteColor];
    self.wkWebView.UIDelegate = self;
    self.wkWebView.navigationDelegate = self;
    [self.wkWebView.scrollView setAlwaysBounceVertical:YES];
    [self.view addSubview:self.wkWebView];
    [self.view addSubview:self.progressView];

    
    [self.wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self.wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    
    NSURL *url= [NSURL URLWithString:formalStrUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.wkWebView loadRequest:request];

}
-(UIProgressView *)progressView{
    
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0,DEF_NAV_HEIGHT,self.view.frame.size.width,0)];
        self.progressView.tintColor = [UIColor blackColor];
        self.progressView.trackTintColor = [UIColor clearColor];
    }
    return _progressView;
}
//计算wkWebView进度条
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
//
    NSLog(@"当前的标题是%@===%@===%@",keyPath,change[@"new"],context);
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.wkWebView.estimatedProgress;
        if (self.progressView.progress == 1) {
            /*
             *添加一个简单的动画，将progressView的Height变为1.4倍，在开始加载网页的代理中会恢复为1.5倍
             *动画时长0.25s，延时0.3s后开始动画
             *动画结束后将progressView隐藏
             */
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                weakSelf.progressView.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;

            }];
        }
    }else{
        
      
        self.navigationItem.title = change[@"new"];
        if ([self.navigationItem.title isEqualToString:@"登录"] || [self.navigationItem.title isEqualToString:@"矿机"] || [self.navigationItem.title isEqualToString:@"我的"]|| [self.navigationItem.title isEqualToString:@"首页"]) {
            
            [self.backButton setHidden:YES];
             
            
        
        
        }else{
            
            [self.backButton setImage:[UIImage imageNamed:@"Back"] forState:UIControlStateNormal];
            [self.backButton setImage:[UIImage imageNamed:@"Back"] forState:UIControlStateHighlighted];
            [self.backButton setHidden:NO];
            
        }
        
        
        
        if ([self.navigationItem.title isEqualToString:@"登录"]) {
            
            self.navigationItem.title = @"";
        }
        
//        if ([self.navigationItem.title isEqualToString:@"首页"]) {
//
//            [self.navigationController.navigationBar setHidden:YES];
//             self.wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height +20)];
//
//
//        }else{
//
//             [self.navigationController.navigationBar setHidden:NO];
//             self.wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
//        }
        

    }
    
}
#pragma mark webview delegate & datasource
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    
    //开始加载网页时展示出progressView
    self.progressView.hidden = NO;
    //开始加载网页的时候将progressView的Height恢复为1.5倍
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    //防止progressView被网页挡住
    [self.view bringSubviewToFront:self.progressView];
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    //加载完成后隐藏progressView
    //    self.progressView.hidden = YES;
}

//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    //加载失败同样需要隐藏progressView
    self.progressView.hidden = YES;
}

#pragma mark 移除观察者
- (void)dealloc
{
    [self.wkWebView removeObserver:self forKeyPath:@"title"];
    [self.wkWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}
- (void)gotoBack{
    
    [self.wkWebView goBack];
}

@end
